package com.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class PayUtil {

    //生成随机外部订单号
    public static String getOrderNo() {
        String[] chars = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
                "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
                "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        StringBuffer stringBuffer = new StringBuffer(DateUtil.getCurrentDateTimeStr1());
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int strInteger = Integer.parseInt(str, 16);
            stringBuffer.append(chars[strInteger % 0x3E]);
        }
        return stringBuffer.toString();
    }

    public static String generateMchPayJsRequestJson(String prepay_id, String appId, String key) {
        String package_ = "prepay_id=" + prepay_id;
        Map<String, String> map = new HashMap();
        map.put("appId", appId);
        map.put("timeStamp", System.currentTimeMillis() / 1000 + "");
        map.put("nonceStr", UUID.randomUUID().toString().replace("-", "").toUpperCase());
        map.put("package", package_);
        map.put("signType", "MD5");
        String paySign = SignatureUtil.generateSign(map, key);
        map.put("paySign", paySign);
        return JsonUtil.toJSONString(map);
    }

    public static Map generateMchPayJsRequestMap(String prepay_id, String appId, String key) {
        String package_ = "prepay_id=" + prepay_id;
        Map<String, String> map = new HashMap();
        map.put("appId", appId);
        map.put("timeStamp", System.currentTimeMillis() / 1000 + "");
        map.put("nonceStr", UUID.randomUUID().toString().replace("-", "").toUpperCase());
        map.put("package", package_);
        map.put("signType", "MD5");
        String paySign = SignatureUtil.generateSign(map, key);
        map.put("paySign", paySign);
        return map;
    }


}